package org.sixtynine.stock.entity;

/**
 * @author Thilanka
 * this is user category 
 *
 */

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "USER_CATEGORY")
public class UserCategory extends BaseEntity {

	private int id;

	@NotEmpty
	private String name;
	private Set<User> user;
	private Set<PackageData> packageData;
	private Set<CategoryModule> categoryModule;

	@Transient
	private List<Module> modules;

	public UserCategory() {
	}

	public UserCategory(String name) {
		this.name = name;
	}

	@Id
	@Column(name = "ID", unique = true, nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "NAME", nullable = false, length = 100)
	public String getName() {
		return name;
	}

	public void setName(String userName) {
		this.name = userName;
	}

	@Override
	public boolean equals(Object obj) {
		return (id == ((UserCategory) obj).getId());
	}

	/*
	 * 
	 * one to many relation ship this is one side annotation.. this mapped with
	 * User class and there is userCategory variable
	 */
	@OneToMany(mappedBy = "userCategory")
	public Set<User> getUser() {
		return user;
	}

	public void setUser(Set<User> user) {
		this.user = user;
	}

	/*
	 * 
	 * one to many relation ship this is one side annotation.. this mapped with
	 * CategoryModule class and there is userCategory variable
	 */
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "userCategory")
	@Cascade({ CascadeType.SAVE_UPDATE, CascadeType.DELETE })
	@Fetch(FetchMode.SELECT)
	@BatchSize(size = 10)
	public Set<CategoryModule> getCategoryModule() {
		if (categoryModule == null) {
			categoryModule = new HashSet<CategoryModule>();
		}

		return categoryModule;
	}

	public void setCategoryModule(Set<CategoryModule> categoryModule) {
		this.categoryModule = categoryModule;
	}

	@Transient
	public List<Module> getModules() {
		if (modules == null) {
			modules = new ArrayList<Module>();
		}
		return modules;
	}

	@Transient
	public void setModules(List<Module> modules) {
		this.modules = modules;
	}

	@OneToMany(mappedBy = "userCategory")
	public Set<PackageData> getPackageData() {
		return packageData;
	}

	public void setPackageData(Set<PackageData> packageData) {
		this.packageData = packageData;
	}

}
